package com.ericsson.enap.modules.ebmLog.task;

import com.ericsson.common.util.LogFileName;
import com.ericsson.common.util.LoggerUtils;
import com.ericsson.enap.modules.ebmLog.entity.EbmEventVO;
import org.slf4j.Logger;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

/**
 * @description: 执行世凯脚本，并生成Log文件任务
 * @author: wangjingshi
 * @create: 2021-04-20 09:32
 */
public class EbmExecuteScriptTask implements Runnable{

    private Logger log = LoggerUtils.Logger(LogFileName.EbmLogJob);
    private String netid;
    private String localPath;
    private CountDownLatch latch;
    private Map<String, EbmEventVO> eventMap;
    private String ebmPath;

    public EbmExecuteScriptTask(String netid, String localPath, CountDownLatch latch, Map<String, EbmEventVO> eventMap,String ebmPath) {
        this.netid = netid;
        this.localPath = localPath;
        this.latch = latch;
        this.eventMap = eventMap;
        this.ebmPath = ebmPath;
    }

    @Override
    public void run() {
        File f = new File(localPath);
        System.out.println("==========localPath==========="+localPath);
        if(!f.exists()){
            f.mkdirs();
        }
        String files[] = f.list();
        System.out.println("==========files==========="+files.length);
        //1、利用世凯新脚本，将下载的所有ebs文件汇总成统一的ebs文件
        String str = "cat";
        for(int i=0;i<files.length;i++) {
            str = str+" "+localPath+files[i];
        }
        String cmd2 = str + " > "+localPath+"total_ebs";
        this.executeCmd(cmd2);
        String total_ebs = "total_ebs";
        String newFiles[] = new File(localPath).list();
        //2、执行世凯脚本，做第一步解析
        for(int i=0;i<newFiles.length;i++) {
            if(newFiles[i].equals(total_ebs)&&!newFiles[i].contains("abnormal")&&!newFiles[i].contains("csv")) {
                String _cmd2 = "/opt/Ericsson/core/bin/count_ebm "+localPath+newFiles[i]+" /opt/Ericsson/core/EbmLog/ebm_offset_c/";
                this.executeCmd(_cmd2);
            }
        }
        log.info(" ===== do first analysis finish total:"+files.length + " ===== "+netid);
        //3、根据事件开启多线程，执行第二部解析，解析所有事件经过世凯脚本筛选出的只包含失败的ebm原始文件(方式跟原来算法相同)
        log.info(" ===== eventMap size:"+eventMap.size());
        CountDownLatch innerlatch = new CountDownLatch(eventMap.size());
        for(Map.Entry<String, EbmEventVO> entry:eventMap.entrySet()) {
            EbmExecuteSheelTask task = new EbmExecuteSheelTask(entry.getKey(),innerlatch,f,localPath,netid,ebmPath);
            new Thread(task).start();
        }
        try {
            innerlatch.await();
            log.info(" ===== analysis ebm script finished ===== " + netid);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            latch.countDown();
        }
    }

    private void executeCmd(String cmd2) {
        Process process = null;
        String command[] = new String[3];
        command[0] = "sh";
        command[1] = "-c";
        command[2] = cmd2;
        try {
            process = Runtime.getRuntime().exec(command);
            try {
                process.waitFor();
            } catch (InterruptedException e) {
                e.printStackTrace();
                log.info(" ===== InterruptedException in EbmExecuteScriptTask =====" + e.getMessage() + " cmd:" + cmd2);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.info(" ===== Exception in EbmExecuteScriptTask =====" + e.getMessage() + " cmd:" + cmd2);
        } finally {
            process.destroy();
        }
    }

}
